<?php
	defined('_JEXEC') or die(); 
    JHTML::_('behavior.tooltip'); 
    $listOrder    = @$this->lists['order'];
    $listDirn    = @$this->lists['order_Dir'];
	$db = JFactory::getDbo();
?>
<style>
    #tbl_filter{
        margin: 0 ; padding: 0 ;
    }
    #tbl_filter td{
        padding: 3px;
    }
    #tbl_filter input, #tbl_filter img {
        float: left;
        margin: 0 5px 0 0;
        width: auto;
    }
    #extra_filter_field { width: 114px; }
    table.adminlist tbody tr td { padding: 8px 8px; }
    table.adminlist thead th { padding: 8px; }
</style>
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.css" />
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery-ui.css" />
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.js"></script>

<script type="text/javascript">
    function submitform(pressbutton){
        var form = document.adminForm ;
        //alert(pressbutton)
        if (pressbutton)
        {
            form.task.value=pressbutton ;
        }
        if ((pressbutton=='send_redemp_notice_email')||(pressbutton=='send_redemp_pick_up_notice')){
              form.controller.value="reports" ;
        }
        form.submit();
    }
	jQuery(function(){
		jQuery("#topDisplaySelect").change(function(){
			jQuery("#limit").val(jQuery(this).val());
			submitform();
		});
		jQuery("#topDisplaySelect").val(jQuery("#limit").val());
	});
    jQuery(function(){ 
        jQuery("#extra_products ").multiselect({
            selectedList: 0,
            noneSelectedText: '-- All --',
            selectedText: "# of # selected"
        }); 
        jQuery("#pick_up_at").multiselect({
            selectedList: 0,
            noneSelectedText: '-- All --',
            selectedText: "# of # selected"
        });
		var status = <?php echo isset($this->status)?$this->status:1;?>;
		jQuery(".status").val(status);
    });
</script>
<div class="row-fluid">
    <?php if (!empty( $this->sidebar)) : ?>
	<div id="j-sidebar-container" class="span2">
		<?php echo $this->sidebar; ?>
	</div>
	<div id="j-main-container" class="span10">
	<?php else : ?>
		<div id="j-main-container">
	<?php endif;?>
		<br clear="all">	
    	<div class="alert">
           <form action="<?php echo JRoute::_("index.php?option=com_wrd")?>" method="post" name="filterform" >
                <table style='margin-bottom:5px' id="tbl_filter">
                    <tbody>
                        <tr>
                            <td align="left" valign="top" style="border-right: 1px solid #d0d0d0;">
                                <table >
                                    <tr>
                                        <td><font title="Chon ngay redemp">  From:</font><br>
                                            <?php
                                                echo JHTML::_ ( 'calendar', ($this->pr['date_from'] != '') ? $this->pr['date_from'] : date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y'))), 'date_from', 'date_from', '%Y-%m-%d');
                                            ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><font title="Chon ngay redemp">To: </font><br>
                                            <?php
                                                echo JHTML::_ ( 'calendar', ($this->pr['date_to'] != '') ? $this->pr['date_to'] : date('Y-m-d', time()), 'date_to', 'date_to', '%Y-%m-%d');
                                            ?>
                                        </td>
                                    </tr>
                                </table>                                
                            </td>
                          
                                                        
                             <td align="left" valign="top" id="statistic_by_job" style="border-right: 1px solid #d0d0d0;">
                                <table>                                  
                                    <tr>
                                        <td>Status:<br>
                                            <select class="status" name="status" id="status">
												<option value="1">Approved</option>
												<option value="0">Waiting Approve</option>
												<option value="-1">All</option>
                                            </select>
                                        </td>
                                    </tr>                                                                                                                                    
                                    <tr>
                                        <td>Product: <br>
                                            <?php echo $this->lists['extra_products'] ; ?>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>Outlet:<br>
                                            <?php echo $this->lists['filter_pick_up_outlet'] ; ?>
                                        </td>
                                    </tr>
                                    
                                </table>
                            </td>
                            
                            <td align="left" valign="top" id="last_filter_column">
                                <table >
                                    <tr>
                                        
                                        <td colspan="2">
                                            Choose: 
                                            <select name="topDisplaySelect" id="topDisplaySelect">
                                                <option value="5">5</option>
												<option value="10">10</option>
												<option value="15">15</option>
												<option value="20">20</option>
                                                <option value="30">30</option>
                                                <option value="50">50</option>
                                                <option value="100">100</option>
                                                <option value="">All</option>
                                            </select>
                                            
                                        </td>
                                    </tr>
                                    <tr valign="top">
                                        <td>&nbsp; </td>
                                        <td style="text-align: left;padding: 10px 3px 0;">
                                            <input type="submit" name="view" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'redemptionreportproduct';" value="View" />
											<input type="submit" name="exportexcel" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'exredempreportproduct';" value="Export to excel" /></td>
                                    </tr>
                                </table>
                            </td>                       
                        </tr>
                    </tbody>
                </table>
                <input type="hidden" name="controller" value="reports" />
                <input type="hidden" name="option" value="com_wrd" />
                <input type="hidden" name="submitViewExcel" id="submitViewExcel" value="" />
                <input type="hidden" name="task" id="task" value="" />
         </form>
         </div> 
            <br />
            <form id="adminForm" action="<?php echo JRoute::_("index.php?option=com_wrd")?>" method="post" name="adminForm" >
                <!--<link type="text/css" href="/js/dialog/css/sunny/jquery-ui-1.8.20.custom--.css" rel="stylesheet" />-->
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/dialog/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript">
    jQuery(function(){ 
        jQuery('.pick-up-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(1);
            jQuery('#dialog').dialog('open');
            return false;
        });
        jQuery('.deliver_to_outlet-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(2);            
            jQuery('#dialog').dialog('open');
            return false;
        });

    });
</script>
<style>
.extra-adminlist strong { color:#025A8D; font-size: 1.2em; font-weight: bold; }
table.extra-adminlist .extra-adminlist-tr td {background: #F7F7F7; }
.filter-detail { border-bottom: 1px solid #CCC; padding-bottom: 3px; font-size: 1.4em; }
.filter-detail span { color: #e72222; }
</style>
<?php 
    $complainType = JHTMLwrd::listComplainTypeRaw();
    $mealPeriod = JHTMLwrd::listMealPeriodRaw();
?>
<script type="text/javascript">
jQuery(function(){
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + "<span style='color:#000;'>Products:</span> " + getSelectedOptionFilter("extra_products")); 
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + ". <span style='color:#000;'>Outlet:</span> " + getSelectedOptionFilter("pick_up_at")); 
});

function getSelectedOptionFilter(listName)
{
    var html = "";
    jQuery("#"+listName+" option[selected=selected]").each(function(){
        html = html + jQuery(this).html() + ", ";
    });
    return html.substring(0, html.length-2);
}
</script>
<h3 class="filter-detail"><span id="statistic_by"></span></h3>
<table class="adminlist extra-adminlist table">
  <thead>
    <tr class="extra-adminlist-tr" >
        <th nowrap="nowrap"><strong>No</strong></th>                   
		<th nowrap="nowrap"><strong>Item</strong></th>	
		<th nowrap="nowrap"><strong>Quantity</strong></th>
		<th nowrap="nowrap"><strong>Redeemption Code</strong></th>	
		<th nowrap="nowrap"><strong>Date Redeem</strong></th>
		<th nowrap="nowrap"><strong>Member ID</strong></th>
		<th nowrap="nowrap"><strong>Name</strong></th>
		<th nowrap="nowrap"><strong>Point Redemp</strong></th>
		<th nowrap="nowrap"><strong>Cash Redemp</strong></th>
		<th nowrap="nowrap"><strong>Retail Pricing</strong></th>
		<th nowrap="nowrap"><strong>Cost Pricing</strong></th>	
		<th style="border: none;" ><strong>Outlet</strong></th>
		<th style="border: none;" align="left"><strong>Percent</strong></th>
		<th style="border: none;"><strong>Charge Cost</strong></th>
    </tr>
  </thead>
  <tbody>
    <?php 
        if(!empty($this->items)):
        {
            $rIds = array();
            $rId = '';
            $previousCode = '';
            $index = '';
            foreach ($this->items as $key => $item):
            
                if($previousCode == ''):    
                    $previousCode = $item->code;
                endif;
                
                if($item->code != $previousCode):
                    if(count($rIds) <= 0):
                        $rIds[0] = trim($rId, ',');
                    else:
                        $rIds[$index] = trim($rId, ',');
                    endif;
                    
                    $rId = '';
                    $index = '';
                endif;
                
                if($index == ''):
                    $index = $key;
                endif;
                $rId .= $item->id.',';    
              
            endforeach;
            $rIds[$index] = trim($rId, ',');
        }
        endif;
     ?>
    <?php 
        if(!empty($this->items)):
           
            $index = 0;
            $i=0;
			//echo '<pre>';print_r($this->items);
            
			foreach($this->items as $key => $item): 
            	$item->product = unserialize($item->product);
            	$item->product = $item->product['en-GB'];
            	
                $conditions['outlet'] = $this->outlet;
				$arr = explode(",",$this->outlet);
				//echo '<pre>';print_r($arr);
				$conditions['code'] = strval($item->code);
				$tr = PointProvider::getTransactionByCode($db, $conditions);
				$total = 0;
				foreach($tr as $its){
					$total += (- $its->point);
				}
				//echo "<pre>";print_r($tr);
				foreach($tr as $row):
			
				if(!empty($arr)&&(in_array($row->outlet,$arr))):
				
                //echo 123123;
				echo '<tr style="cursor:pointer;" class="row'.(($i+1)%2).'" valign="top">';
                    $i++;
                    echo '<td  style="">'.$i.'</td>';   
					echo '<td  style="width:10%">'.$item->product.'</td>'; 
					echo '<td style="text-align:right;">'.$item->quantity .'</td>';
					echo '<td style="text-align:right;">'.$item->code .'</td>';
					echo '<td style="text-align:right;">'.$item->exchange_date .'</td>';
					echo '<td style="text-align:right;">'.$item->member_id .'</td>';
					echo '<td style="text-align:right;">'.$item->l_name. " ".$item->f_name .'</td>';
					echo '<td style="text-align:right;">'.number_format($item->point,0,".",",") .'</td>';
					echo '<td style="text-align:right;">'.number_format($item->cash,0,".",",") .'</td>';
					echo '<td style="text-align:right;">'.number_format($item->price,0,".",",") .'</td>';
					echo '<td style="text-align:right;">'.number_format($item->cost_pricing,0,".",",") .'</td>';					
                   
					echo '<td>';
								if($row->name != ''){
									echo $row->name;
								} 
								else if(($row->type == 9) && ($row->transaction == 0)){
									echo "WMCRewards - Start Point";
								}
								else if(($row->type == 0) &&($row->transaction == 0)){
									echo "WMCRewards";
								}
								else {
									$db = JFactory::getDbo();
									if(empty($row->name)){
										$sql = "SELECT des,MIN(id) FROM #__wrd_point WHERE transaction=".$row->transaction;
										$db->setQuery($sql);
										$des = $db->loadObjectList();
										echo "WMC GROUP - ".$des[0]->des;
									}
								}
						echo '</td><td>';
								echo number_format((-($row->point)/$total * 100),2, '.', ',') ."%";
						echo '</td><td align="right">';
								echo number_format($item->quantity * $item->cost_pricing*(-($row->point)/$total),0, '.', ',');
						echo '</td>';	
					
					
                echo '</tr>';
				
				elseif(empty($arr[0])) :
				
				echo '<tr style="cursor:pointer;" class="row'.(($i+1)%2).'" valign="top">';
                    $i++;
                    echo '<td  style="">'.$i.'</td>';   
					echo '<td  style="width:10%">'.$item->product.'</td>'; 
					echo '<td style="text-align:right;">'.$item->quantity .'</td>';
					echo '<td style="text-align:right;">'.$item->code .'</td>';
					echo '<td style="text-align:right;">'.$item->exchange_date .'</td>';
					echo '<td style="text-align:right;">'.$item->member_id .'</td>';
					echo '<td style="text-align:right;">'.$item->l_name. " ".$item->f_name .'</td>';
					echo '<td style="text-align:right;">'.number_format($item->point,0,".",",").'</td>';
					echo '<td style="text-align:right;">'.$item->price .'</td>';
					echo '<td style="text-align:right;">'.$item->cost_pricing .'</td>';					
                   
					echo '<td>';
								if($row->name != ''){
									echo $row->name;
								} 
								else if(($row->type == 9) && ($row->transaction == 0)){
									echo "WMCRewards - Start Point";
								}
								else if(($row->type == 0) &&($row->transaction == 0)){
									echo "WMCRewards";
								}
								else {
									$db = JFactory::getDbo();
									if(empty($row->name)){
										$sql = "SELECT des,MIN(id) FROM #__wrd_point WHERE transaction=".$row->transaction;
										$db->setQuery($sql);
										$des = $db->loadObjectList();
										echo "WMC GROUP - ".$des[0]->des;
									}
								}
						echo '</td><td>';
								echo number_format((-($row->point)/$total * 100),2, '.', ',') ."%";
						echo '</td><td align="right">';
								echo number_format($item->quantity * $item->cost_pricing*(-($row->point)/$total),0, '.', ',');
						echo '</td>';	
					
                echo '</tr>';
				endif;
              endforeach;
            endforeach;
        endif;
    ?>
    
  </tbody>
  <tfoot>
        <tr>
            <td colspan="16">
                <?php
                     echo $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
</table>
                <input type="hidden" name="controller" value="reports" />
                <input type="hidden" name="option" value="com_wrd" />
				<input type="hidden" class="status" name="status" value="" />
                <input type="hidden" name="task" value="redemptionreportproduct" />
                <?php echo JHTML::_( 'form.token' ); ?>
            </form>
        </div>
        
</div>